<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;



use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\SendMsgModel;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use app\crm\model\ReserveandLost;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\common\traits\ActionRecordTrait;

use app\shop\model\unionpay\AppConfig;
use app\shop\model\unionpay\AppUtil;
use app\shop\model\unionpay\Test;
use app\crm\model\MiniProgramModel;
use app\shop\model\RefundPayModel;


class ZTdaochu extends ApiCommon
{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['getShopIds','puji','expend'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    public function getShopIds()
    {
        $where = [];

        array_push($where,['type','in',[1,3]]);
        array_push($where,['shop_name','not in','营销中心']);
        array_push($where,['status','=','1']);

        $shop_list = DB::name('shop')->where($where)->column('shop_name,market_name','shop_id');

        return $shop_list;
    }

    public function puji()
    {
        // echo "普及";

        $one_year_ago = strtotime('- 1year');

        $date = date('Y-m-d');

        $shop_ids = $this->getShopIds();

        $model = new ReserveandLost();

        $list = [];
        foreach ($shop_ids as $key => $value) {

            $customer_ids = $model->cj_customer([$key],[]);

            $baoyou_ids = $model->baoyou_customer($customer_ids,$date);


            $where = [];

            array_push($where,['uid','in',$baoyou_ids]);
            array_push($where,['deal_time','>=',$one_year_ago]);
            array_push($where,['business_type','in',[2,4]]);
            array_push($where,['status','=','2']);
            array_push($where,['type','=','3']);
            array_push($where,['refund_type','=','101.02']);

            // field(['SUM(pay_amount) as pay_amount'])->select()->toArray();
            $res = Db::name('shop_refund_order')->where($where)->group('uid')->column('SUM(pay_amount) as pay_amount','uid');

            $pujiNum = 0;
            foreach ($res as $k => $val) {
                if($val >= '500.00'){
                    $pujiNum++;
                }
            }
            

            $one = [];

            $one['market_name'] = $value['market_name'];
            $one['shop_name'] = $value['shop_name'];
            $one['baoyou'] = count($baoyou_ids);
            $one['pujiNum'] = $pujiNum;

            array_push($list,$one);
            
        }

        $callName = [

            'market_name' =>'门店',

            'shop_name' =>'门店',

            'baoyou' => '活客人数',

            'pujiNum' => '普及人数',

            '' => '普及率',
            
        ];

        $downUrl = exportExcelData('项目普及率统计',$callName,$list,'xlsx',1);

        echo "<script language='javascript'>";

        echo " location='".$downUrl."';";

        echo "</script>";
    }

    public function expend()
    {
        echo "消耗统计";

        $request = request()->param();

        echo "<pre>";
        print_r($request);

        exit;

        $start_time = strtotime(date('Ym01',time()));

        $end_time = strtotime(date('Ymt',time()))+86399;

        $shop_ids = $this->getShopIds();

        $list = [];
        foreach ($shop_ids as $key => $value) {

            $where = [];

            array_push($where,['create_time','between',"$start_time,$end_time"]);
            array_push($where,['shop_id','=',$key]);

            // echo "<pre>";
            // print_r($where);
            // exit;

            // 人数
            $customerIds = Db::name('service_log')->where($where)->column('DISTINCT(customer_id) AS customer_id');

            // echo "<pre>";
            // print_r($customerIds);
            // exit;


            // 次数
            $num = 0;
            foreach ($customerIds as $k => $v) {

                $where = [];

                array_push($where,['create_time','between',"$start_time,$end_time"]);
                array_push($where,['customer_id','=',$v]);

                $log_time = Db::name('service_log')->where($where)->column('DISTINCT(create_time) AS create_time');

                // echo "<pre>";
                // print_r($log_time);

                $date_list = [];
                foreach ($log_time as $key1 => $value1) {
                    $date = date('Ymd',$value1);

                    if(in_array($date,$date_list)){
                        
                    }else{
                        array_push($date_list,$date);
                    }
                    
                }

                
                
                $num = $num + count($date_list);
            }

            $where = [];

            array_push($where,['create_time','between',"$start_time,$end_time"]);
            array_push($where,['shop_id','=',$key]);

            $countNum = Db::name('service_log')->where($where)->count();

            $one = [];

            $one['shop_name'] = $value['shop_name'];
            $one['customer_id'] = count($customerIds);
            $one['num'] = $num;
            $one['countNum'] = $countNum;


            array_push($list,$one);

        }

        echo "<pre>";
        print_r($list);
        // exit;

        $callName = [

            'shop_name' =>'门店',

            'customer_id' => '人头数',

            'num' => '次数',

            'countNum' => '个数',
            
        ];

        $downUrl = exportExcelData('护理统计',$callName,$list,'xlsx',1);

        echo "<script language='javascript'>";

        echo " location='".$downUrl."';";

        echo "</script>";
        
    }

}